package com.acheli.registry.network.packet;

import com.acheli.ACHeli;
import com.acheli.registry.config.CustomDataBus;
import com.acheli.registry.network.ACHMessages;
import com.acheli.rideable.entites.ComponentEntity;
import com.acheli.rideable.entites.RideableEntity;
import com.acheli.rideable.functionLathes.ComponentFL;
import com.acheli.rideable.functionLathes.component.DefaultComponentProvider;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.simple.SimpleChannel;

import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Supplier;

public class ComponentSetupC2SPacket {
    // 添加字符串字段
    private final CompoundTag tag;

    // 从字节缓冲区解码
    public ComponentSetupC2SPacket(FriendlyByteBuf buf) {
        this.tag = buf.readNbt();
    }

    public ComponentSetupC2SPacket(UUID rideableUUID, UUID componentUUID, String ID) {
        tag = new CompoundTag();
        tag.putString("ID", ID);
        tag.putUUID("rideableUUID", rideableUUID);
        tag.putUUID("componentUUID", componentUUID);
    }

    // 编码到字节缓冲区
    public void toBytes(FriendlyByteBuf buf) {
        buf.writeNbt(tag);
    }

    private boolean handle(Supplier<NetworkEvent.Context> supplier) {
        String ID = tag.getString("ID");
        UUID rideableUUID = tag.getUUID("rideableUUID");
        UUID componentUUID = tag.getUUID("componentUUID");
        ACHeli.LOGGER.info("ComponentSetupC2SPacket => ID: " + ID);
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(() -> {
            ServerPlayer player = context.getSender();
            if (player != null) {
                ServerLevel level = player.serverLevel();
                if  (level.getEntity(rideableUUID) instanceof RideableEntity rideable) {
                    if  (level.getEntity(componentUUID) instanceof ComponentEntity component) {
                        ComponentFL fl = rideable.factory
                                .getProductFunction()
                                .getFL()
                                .getComponentFLByID(ID); //尝试静态注册

                        if (fl == null) { // 是动态注册
                            //从gson配置库扫描
                            List<DefaultComponentProvider> providerList =
                                    CustomDataBus.gsonDefaultComponentProvidersMap.get(rideable.getFactoryName());
                            for (DefaultComponentProvider provider : providerList) {
                                if (Objects.equals(provider.getID(), ID)) {
                                    fl = provider.get(rideable);
                                }
                            }
                        }
                        component.setUp(rideable, fl);
                    }
                }

            }
        });
        return true;
    }

    public static void register(SimpleChannel channel) {
        channel.messageBuilder(ComponentSetupC2SPacket.class, ACHMessages.id(), NetworkDirection.PLAY_TO_SERVER)
                .decoder(ComponentSetupC2SPacket::new)
                .encoder(ComponentSetupC2SPacket::toBytes)
                .consumerMainThread(ComponentSetupC2SPacket::handle)
                .add();
    }
}